<?php
class Usuario extends AppModel {
	
	var $belongsTo = array('Grupo');
	var $displayField = 'nome';	
	
	var $validate = array(
        'nome' => array(
            'vazio' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'message' => 'Nome deve ser informado!',
                'last' => true
			)
		),
		
        'username' => array(
            'vazio' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'message' => 'Login deve ser informado!',
                'last' => true
			)
		),

        'password' => array(
            'vazio' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'message' => 'Senha deve ser informado!',
				'on' => 'create',
                'last' => true
			)
		),
				
        'grupo_id' => array(
            'vazio' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'message' => 'Grupo deve ser informado!',
                'last' => true
			)
		)		
	);	

    public function beforeSave($options = array()) {

    	$this->data['Usuario']['username'] = strtolower($this->data['Usuario']['username']);
    	$this->data['Usuario']['username'] = str_replace(' ', '-', $this->data['Usuario']['username']);
    	
    	if (isset($this->data['Usuario']['password'])) {
        	$this->data['Usuario']['password'] = AuthComponent::password($this->data['Usuario']['password']);
    	}

        parent::beforeSave($options);
    }	
	
}
